﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace FlanProjectData.Helpers
{
    public static class CollisionHelper
    {
        // Per-pixel collision detection
        public static bool IntersectPixels(Rectangle firstRectangle, Color[] firstData, Rectangle secondRectangle, Color[] secondData)
        {
            // Find the bounds of the rectangle intersection
            var top = Math.Max(firstRectangle.Top, secondRectangle.Top);
            var bottom = Math.Min(firstRectangle.Bottom, secondRectangle.Bottom);
            var left = Math.Max(firstRectangle.Left, secondRectangle.Left);
            var right = Math.Min(firstRectangle.Right, secondRectangle.Right);

            // Check every point within the intersection bounds
            for (var y = top; y < bottom; y++)
            {
                for (var x = left; x < right; x++)
                {
                    // Get the color of both pixels at this point
                    var colorA = firstData[(x - firstRectangle.Left) + (y - firstRectangle.Top) * firstRectangle.Width];
                    var colorB = secondData[(x - secondRectangle.Left) + (y - secondRectangle.Top) * secondRectangle.Width];

                    // If both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        // then an intersection has been found
                        return true;
                    }
                }
            }

            // No intersection found
            return false;
        }
    }
}
